/**
 * 文 件 名:  SiteController.java
 * 版      权:  财之道  Copyright ©2011-2015 All Rights Reserved
 * 描      述:  
 */
package com.czd.support.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.czd.base.model.Paginate;
import com.czd.support.common.Configuration;
import com.czd.support.model.Site;
import com.czd.support.service.SiteService;

/**
 * @Description 站点控制类
 * 
 * @Author Jun.Yang
 * @Date 2015年3月9日 下午1:15:33
 * @Version v1.0
 */
@Controller
@RequestMapping("/site")
public class SiteController extends BaseController {
	@Autowired
	private SiteService siteService;

	@RequestMapping(value = { "", "/", "/list" })
	public String list(
			Site site,
			@RequestParam(value = "pageNo", defaultValue = "1", required = false) int pageNo,
			Model model) {
		site.setPageNo(pageNo);
		site.setPageSize(Configuration.getInstance().getDefault_page_size());

		Paginate paginate = siteService.queryForPage(site);

		model.addAttribute("paginate", paginate);

		return "site/list";
	}

	/**
	 * 跳转到站点信息添加页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String add() {
		return "site/add";
	}

	/**
	 * 站点信息添加
	 * 
	 * @param request
	 * @param site
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String add(HttpServletRequest request, Site site, Model model) {
		if (siteService.insert(site)) {
			model.addAttribute("site", site);
			return "redirect:/site/" + site.getId() + "/details";
		}

		return error(model, "操作失败", "添加站点信息失败");
	}

	/**
	 * 跳转到站点信息编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
	public String edit(@PathVariable int id, Model model) {
		Site site = siteService.findById(id);

		if (site != null) {
			model.addAttribute("site", site);

			return "site/edit";
		}

		return error(model, "操作失败", "站点信息不存在");
	}

	/**
	 * 站点信息编辑
	 * 
	 * @param request
	 * @param site
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	public String edit(HttpServletRequest request, Site site, Model model) {
		if (siteService.update(site)) {
			model.addAttribute("site", site);
			return "redirect:/site/" + site.getId() + "/details";
		}

		return error(model, "操作失败", "修改站点信息失败");
	}

	@RequestMapping(value = "{id}/delete", method = RequestMethod.GET)
	public String delete(@PathVariable int id, Model model) {
		if (siteService.delete(id)) {
			return "redirect: /site/list";
		}

		return error(model, "操作失败", "删除站点信息失败");
	}

	/**
	 * 站点信息详情
	 * 
	 * @param request
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
	public String details(HttpServletRequest request, @PathVariable int id,
			Model model) {
		Site site = siteService.findById(id);

		if (site != null) {
			model.addAttribute("site", site);
			return "site/details";
		}

		return error(model, "操作失败", "站点信息不存在");
	}
}
